﻿using AutoMapper;
using B.S.AuthCenter.Api.Write.Application.Command.Approval;
using B.S.AuthCenter.Domain.Form;
using B.S.AuthCenter.ErrorCount;
using B.S.AuthCenter.InfrasIntructure.Interface;
using MediatR;

namespace B.S.AuthCenter.Api.Write.Application.Handler.Approval
{
    public class UpdateAppFormHandler : IRequestHandler<UpdateAppFormCommand, ApiResult<int>>
    {
        IBaseRepository<AppFormModel> appRepo;
        IMapper mapper;

        public UpdateAppFormHandler(IBaseRepository<AppFormModel> appRepo, IMapper mapper)
        {
            this.appRepo = appRepo;
            this.mapper = mapper;
        }

        public Task<ApiResult<int>> Handle(UpdateAppFormCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();
           
            var menus = appRepo.GetAll().Where(x => !x.IsDeleted && x.Id == request.Id).FirstOrDefault();
            if (menus == null)
            {
                res.Msg = "找不到编号";
                res.Code = ApiEnum.Fail;
                return Task.FromResult(res);
            }
            menus = mapper.Map(request, menus);
           
            int a = appRepo.Update(menus).Result;
            if (a > 0)
            {
                res.Msg = "修改申请成功";
                res.Code = ApiEnum.Success;
                res.Datas = a;

            }
            else
            {
                res.Msg = "修改申请失败";
                res.Code = ApiEnum.Fail;
                res.Datas = a;
            }
            return Task.FromResult(res);
        }
    }
}
